var app=new Vue({
    el:'.mtcgexchange',
    computed:{
        exchangenum:{//兑换数量
            get(){
                if(!isNaN(Number(this.exchange.exchangenum))){
                    return this.exchange.exchangenum
                }else{
                    return 0
                }
            },
            set(v){ 
                if(!isNaN(Number(v))){
                    if(parseFloat(Number(v))<0){
                        this.exchange.exchangenum=0
                    }else{
                        this.exchange.exchangenum=parseFloat(v)
                    }
                    if(parseFloat(Number(v))>parseFloat(this.userinfo.banlance)){
                        this.exchange.exchangenum=parseInt(this.userinfo.banlance)
                    }
                    let computedresult=parseFloat(Number(this.exchange.exchangenum))*this.exchange.agratenum
                    if(!isNaN(Number(computedresult))){
                        this.exchange.exchangeagcoin=computedresult
                    }else{
                        this.exchange.exchangeagcoin=0
                    }
                }else{
                    this.cashout.exchangeagcoin=0
                }
            }
        },
        cashoutnum:{ //兑出数量
            get(){
                if(!isNaN(Number(this.cashout.exchangenum))){
                    if(parseFloat(this.cashout.exchangenum)>=0){
                        return this.cashout.exchangenum
                    }else{
                        return 0
                    }
                }else{
                    return 0
                }
            },
            set(v){ 
                if(!isNaN(Number(v))){
                    if(parseFloat(Number(v))<0){
                        this.cashout.exchangenum=0
                    }else{
                        this.cashout.exchangenum=parseInt(v)
                    }
                    if(parseFloat(Number(v))>parseFloat(this.userinfo.myagbanlance)){
                        this.cashout.exchangenum=parseInt(this.userinfo.myagbanlance)
                    }
                    let computedresult=parseFloat(Number(this.cashout.exchangenum))/parseFloat(this.exchange.agratenum)
                    computedresult=parseFloat(computedresult.toFixed(8)) 
                    if(!isNaN(Number(computedresult))){
                        this.cashout.exchangeagcoin=computedresult
                    }else{
                        this.cashout.exchangeagcoin=0
                    }
                }else{
                    this.cashout.exchangeagcoin=0
                }
            }
        }
    },
    data(){
        return {
            showtransforminag:true,//兑换AG分
            showtransformout:false,//兑出AG分
            exchange:{ //兑换配置项
                defaultcoin:'HPS', //默认币种
                isshowcoinarr:false, //是否展示下拉选
                exchangenum:'0', //兑换数量
                exchangeagcoin:'0',//兑换后数量
                agratenum:5, //AG兑换比例
                disabledbtn:false //禁用兑换点击按钮
            },
            cashout:{ //兑出配置项
                exchangenum:'0', //兑换数量
                exchangeagcoin:'0',//兑换后币种数量
                disabledbtn:false //禁用兑出点击按钮
            },
            userinfo:{ //用户信息
                banlance:'',//用户余额
                myagbanlance:0,//我的ag积分余额
            }
        }
    },
    created() {
        this.setRem(27)
        this.getagBalance()
        this.getagDict(this.exchange.defaultcoin)
    },
    methods:{
        setRem(itemNum) { //设置字体rem
            let size = itemNum || 36;
            window.onresize = remSize;
            remSize();
            function remSize() {
                let html = document.getElementsByTagName('html')[0], w = html.clientWidth;
                w = w > 768 ? 768 : w;
                html.style.fontSize = w / size + 'px'
            }
        },
        getagBalance() {//获取当前用户TCG分
            let params = {
                coin: this.exchange.defaultcoin
            }
            axios.post('/tcgBalance', params).then((res) => {
                if (res.data.code == 200) {
                    if(res.data.data.status==0){
                        this.userinfo.myagbanlance = res.data.data.balance
                    }
                }
            })
        },
        getagDict(coinname) { //选择币种获取兑换比例
            if (!coinname) {
                return
            }
            axios.get(`/ag/dict/${coinname}`, {}).then((res) => {
                this.exchange.agratenum = parseFloat(res.data.data.dictValue)
            })
        },
        changeexchangecoin(cointype,banlance){//用户点击更换币种
            this.exchange.defaultcoin=cointype
            this.exchange.isshowcoinarr=false
            this.userinfo.banlance=banlance
            this.getagDict(cointype)
            this.getagBalance()
        },
        tcgtabchange(keyname){ //更换兑换、兑回tab标签页
            this.$data[keyname]=true
            if(keyname=='showtransforminag'){
                this.showtransformout=false
            }else if(keyname=='showtransformout'){
                this.showtransforminag=false
            }
        },
        confirmexchange(){ //确定兑换TCG分
            //为空、NaN、兑换数量>用户余额
            if(
                this.exchange.exchangenum==''||isNaN(Number(this.exchange.exchangenum))||this.exchange.exchangenum==0||
                parseFloat(Number(this.exchange.exchangenum))>parseFloat(this.userinfo.banlance)
            ){
                return 
            }
            this.exchange.disabledbtn=true
            let params = {
                coin: this.exchange.defaultcoin,
                amount: parseFloat(this.exchange.exchangenum),
                type:1
            }
            axios.post('/tcgTransfer',params).then((res) => {
                this.exchange.disabledbtn=false
                if(res.data.code==200){
                    if(res.data.data.status==0){
                        this.getagBalance()
                    }else{
                        this.getagBalance()
                    }
                }
            })
        },
        confircashout(){ //从TCG兑回
            if(
                localStorage.getItem('ID')==''||
                localStorage.getItem('ID')==''
            ){
                location.href = '/singin'
                return 
            }
            if(
                this.userinfo.myagbanlance==''||
                this.cashout.exchangenum==0||
                this.cashout.exchangenum==''||
                isNaN(Number(this.userinfo.myagbanlance))||
                parseFloat(this.userinfo.myagbanlance)<=0
            ){
                return 
            }
            this.cashout.disabledbtn=true
            let params = {
                coin: this.exchange.defaultcoin,
                amount: parseFloat(this.cashout.exchangenum),
                type: 2
            }
            axios.post('/tcgTransfer',params).then((res) => {
                this.cashout.disabledbtn=false
                if(res.data.code==200){
                    this.getagBalance()
                }
            })
        }
    }
})